﻿/*
 * 下载文件窗体。
 * 该窗体没有页面内容，纯粹用于下载一个文件。需要在查询字符串中给定file_id值。
 * 
 *Create: 2009-4-1 SZJ
 * 
 * 
 * */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using EQT.Dal;
using EQT.Model;

public partial class getfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string file_id = Request.QueryString["fileid"];

            if (!String.IsNullOrEmpty(file_id))
            {
                DaTfile da = new DaTfile(DaCommon.DefaultDb);
                MoTfile mo= da.GetEntity(file_id);
                if (mo != null)
                {
                    string file=Utility.GetLocalFileFromUri(mo.File_Url);
                    System.IO.FileInfo fi = new System.IO.FileInfo(file);
                    if (fi.Exists)
                    {
                        Response.Clear();
                        Response.AddHeader("Content-Length",fi.Length.ToString());
                        Response.ContentType = "application/octet-stream";
                        Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(mo.File_Name));
                        
                        Response.TransmitFile(file);                        
                        Response.End();
                        Response.Close();
                    }
                    else
                    {
                        Utility.SiteError(String.Format("{0}不存在.", mo));
                    }
                }

            }
        }
    }
}
